package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CateGoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @program: sky-take-out
 * @description:
 * @author: fmp
 * @create: 2023-04-09 15:10
 * @Version 1.0
 **/
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService{
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private DishMapper  dishMapper;
    @Resource
    private SetMealMapper setMealMapper;
    //分类条件查询
    @Override
    public PageResult SelectPageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category> pageResult = categoryMapper.SelectPageQuery(categoryPageQueryDTO);
        long total = pageResult.getTotal();
        List<Category> result = pageResult.getResult();
        return PageResult.builder().total(total).records(result).build();
    }
    //新增分类
    @Override
    @CacheEvict(cacheNames = "Category",allEntries = true)
    public void InsertCategory(CateGoryDTO cateGoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(cateGoryDTO, category);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setStatus(StatusConstant.ENABLE);
        categoryMapper.InsertCategory(category);

    }
    //更新分类信息
    @Override
    @CacheEvict(cacheNames = "Category",allEntries = true)
    public void UpdateCategory(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        log.info("gengxin xinxi ======{}",category.toString());
        categoryMapper.UpdateCategory(category);
    }
    //根据id删除分类信息
    @Override
    @CacheEvict(cacheNames = "Category",allEntries = true)
    @Transactional
    public void DeleteCategoryById(Long id) {
        if (dishMapper.selectById(id)!=0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        if (setMealMapper.selectById(id)!=0){
          throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.DeleteCategoryById(id);

    }
    //根据类型查询分类
    @Override
    public List<Category> SelectCategoryByType(CategoryPageQueryDTO categoryPageQueryDTO) {
        List<Category> categoryList = categoryMapper.SelectPageQuery(categoryPageQueryDTO);
        return categoryList;
    }
    /**
     * 查询指定类型的分类信息
     *
     * @param type 分类类型(菜品分类/套餐分类)
     * @return 对应分类类型的分类信息
     */
    @Override
    public List<Category> selectByType(Integer type) {
        return categoryMapper.selectByType(type);
    }
}
